/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.data.clbuffer;

import com.nativelibs4java.opencl.CLBuffer;
import com.nativelibs4java.opencl.CLContext;
import com.nativelibs4java.opencl.CLMem;
import com.nativelibs4java.opencl.CLQueue;
import org.bridj.Pointer;
import technotheist.data.BBuffer;
import technotheist.data.BufferManager;

/**
 *
 * @author Mitch
 */
public class CLBufferManager extends BufferManager {

    private CLQueue queue;
    private CLContext context;

    public CLBufferManager(CLQueue queue, CLContext context) {
	this.queue = queue;
	this.context = context;
    }

    protected <T> void write(Pointer<T> ptr, CLBuffer<T> buffer) {
	buffer.write(queue, ptr, true);
    }

    protected <T> void read(Pointer<T> ptr, CLBuffer<T> buffer) {
	buffer.read(queue, ptr, true);
    }

    protected <T> CLBuffer<T> createBuffer(Pointer<T> ptr, int size) {
	return context.createBuffer(CLMem.Usage.InputOutput, ptr);
    }

    protected Pointer<Float> createFloatPtr(int size) {
	return Pointer.allocateFloats(size);
    }

    protected Pointer<Double> createDoublePtr(int size) {
	return Pointer.allocateDoubles(size);
    }

    protected Pointer<Short> createShortPtr(int size) {
	return Pointer.allocateShorts(size);
    }

    protected Pointer<Integer> createIntPtr(int size) {
	return Pointer.allocateInts(size);
    }

    protected Pointer<Long> createLongPtr(int size) {
	return Pointer.allocateLongs(size);
    }

    protected Pointer<Byte> createBytePtr(int size) {
	return Pointer.allocateBytes(size);
    }

    @Override
    public BBuffer<Float> createFloatBuffer(int size) {
	return new CLFloatBuffer(this, size);
    }

    @Override
    public BBuffer<Double> createDoubleBuffer(int size) {
	return new CLDoubleBuffer(this, size);
    }

    @Override
    public BBuffer<Integer> createIntBuffer(int size) {
	return new CLIntBuffer(this, size);
    }

    @Override
    public BBuffer<Byte> createByteBuffer(int size) {
	return new CLByteBuffer(this, size);
    }

    @Override
    public BBuffer<Short> createShortBuffer(int size) {
	return new CLShortBuffer(this, size);
    }

    @Override
    public BBuffer<Long> createLongBuffer(int size) {
	return new CLLongBuffer(this, size);
    }

}
